var TableGoods = function () {
    return {
        init: function () {
		    function bestStatus(val){
				var ids = [];
				$('.checkboxes').each(function(){
					if($(this).prop('checked')) ids.push($(this).val());
				})				
				alert(ids);
				layer.confirm('确定设置为精品？',function(index){
					$.post(ajaxUrl,{id:ids.join(','),ajax:'bestStatus',status:val},function(data){
						if(data){
							layer.msg('所选商品已设置为精品！',{icon: 1,time: 2000});
							oTable.fnReloadAjax();
							
						}else{
							layer.msg('设置精品失败！',{icon: 2,time: 2000});
							return;
						}
				   });					
				});	
			}     
            var oTable = $('#table_common').dataTable({
                "aLengthMenu": [
                    [-1,5, 15, 20],
                    ["全部",'5条', '15条', '20条'] 
                ],
                "iDisplayLength": 15,
				"serverSide":true,
				//"aaSorting":[[6,'desc']],
                "sPaginationType": "bootstrap",
				//"bStateSave": true, 
                "oLanguage": {
					"sSearch":"搜索",
                    "sLengthMenu": "每页显示 _MENU_ 记录",    
					"sEmptyTable":"没有数据",
					"sInfo": "当前数据为从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录",   
					"sInfoEmpty": "没有数据",   
					"sInfoFiltered" : "",
					"sInfoPostFix" : "",
					"sLoadingRecords":"",
					"sProcessing": "正在加载数据...", 
					"sZeroRecords": "没有检索到数据", 
                    "oPaginate": {   
						"sFirst": "首页",   
						"sPrevious": "上一页",   
						"sNext": "下一页",   
						"sLast": "尾页"  
					}   
                },
				"sAjaxSource":ajaxUrl,
				"sAjaxDataProp": "data",
				//"sDom":'l<"actions" f>rtip',
				"aoColumns": [
	
					{"sTitle":'<input type="checkbox" class="group-checkable" data-set="#table_common .checkboxes"/>',"mDataProp": 'goodsId'},
					{"sTitle":'商品图',"mDataProp": 'goodsImg'},
					{"sTitle":'商品名称',"mDataProp": 'goodsName'},
					{"sTitle":'商品编号',"mDataProp": 'goodsSn'},
					{"sTitle":'价格',"mDataProp": 'shopPrice'},
					{"sTitle":'销量',"mDataProp": 'saleCount'},
					{"sTitle":'店铺',"mDataProp": 'shopName'},
					{"sTitle":'分类',"mDataProp": 'catName'},
					{"sTitle":'推荐',"mDataProp": 'isAdminRecom'},
					{"sTitle":'精品',"mDataProp": 'isAdminBest'},
					{"sTitle":'操作',"mDataProp": 'goodsFlag'}
				],
                "aoColumnDefs": [{
                        'bSortable': false,
						'aTargets': ['_all']
                },
				{"aTargets":[0],"mRender":function(data,type,full){
					return '<input type="checkbox" class="checkboxes" value="'+data+'"/>';
				},"sClass":'text-center',"sWidth":'5%'},
				{"aTargets":[1],"mRender":function(data,type,full){	
					data=data?data:goodsImage;
					return '<img src="'+Upload+'/'+data.replace(".","_s.")+'"/>';												
                    },"sClass":'text-center',"sWidth":'5%'},
				/* {"aTargets":[0],"mRender":function(data,type,full){
					if(full['isOnline']==1){
						if(full['enabled']==1){						
							return data+' <a class="delpay btn btn-xs default" href=""><i class="fa fa-cog"></i> 卸载</a>';						
						}else{
							return data+' <a class="editpay btn btn-xs blue" href=""><i class="fa fa-cog"></i> 安装</a>';
						}						
                    }else{
						return data;
					}
				}},
				{"aTargets":[4],"mRender":function(data,type,full){						
					if(data==1){						
						return '<span class="isonline btn btn-xs blue">在线支付</span>';						
					}else{
						return '<span class="online btn btn-xs default">线下支付</span>';
					}						
                    },"sClass":'text-center'}, */
				{"aTargets":[4],"mRender":function(data,type,full){
					return '<span class="text-danger"><strong>&yen; '+data+'</strong></span>';
				},"sClass":'text-center',"sWidth":'5%'},
				{"aTargets":[8],"mRender":function(data,type,full){						
					if(data==1){						
						return '<span class="btn btn-xs blue">已推荐</span>';						
					}else{
						return '<span class="btn btn-xs default">未推荐</span>';
					}						
                    },"sClass":'text-center'},
				{"aTargets":[9],"mRender":function(data,type,full){						
					if(data==1){						
						return '<span class="btn btn-xs yellow">精品</span>';						
					}else{
						return '<span class="btn btn-xs default">普通</span>';
					}						
                    },"sClass":'text-center'},
				{"aTargets":[10],"mRender":function(data,type,full){
					if(full['goodsStatus']==1){
						html =  '<a class="no btn btn-xs red" href=""><i class="fa fa-ban"></i> 禁售</a>';
					}else{
						html =  '<a class="no btn btn-xs green" href=""><i class="fa fa-check"></i> 通过</a>';
					}
					
					return '<a class="view btn btn-xs default" href=""><i class="fa fa-eye"></i> 详情</a>'+html;
                    },"sClass":'text-center',"sWidth":'5%'}
				],
				"fnCreatedRow":function(nRow,aData,iDataIndex){
					//$('td:eq(0)',nRow).html(iDataIndex+1);//为第一个单元格增加序号
				}				
            });
			
			jQuery('#table_common .group-checkable').change(function () {
                var set = jQuery(this).attr("data-set");
                var checked = jQuery(this).is(":checked");
                jQuery(set).each(function () {
                    if (checked) {
                        $(this).attr("checked", true);
                        $(this).parents('tr').addClass("active");
                    } else {
                        $(this).attr("checked", false);
                        $(this).parents('tr').removeClass("active");
                    }                    
                });
                jQuery.uniform.update(set);
            });

            jQuery('#table_common').on('change', 'tbody tr .checkboxes', function(){
                 $(this).parents('tr').toggleClass("active");
            });

            jQuery('#table_common_wrapper .dataTables_filter input').addClass("form-control input-medium input-inline"); // modify table search input
            jQuery('#table_common_wrapper .dataTables_length select').addClass("form-control input-small"); // modify table per page dropdown
            jQuery('#table_common_wrapper .dataTables_length select').select2({
                showSearchInput : false 
            }); 
			
			var nEditing = null;
			
			html=	' <span> <button class="recom btn blue"> 推荐</button>';
			html+=	' <button class="noRecom btn default"> 取消</button>';
			html+=	' <button class="best btn yellow"> 精品</button>';
			html+=	' <button class="noBest btn default"> 普通</button></span>';
			$(html).find('button').click(function(){setStatus(this);}).end().insertAfter('#table_common_filter input');
			//设置状态
			function setStatus(a){
				var setName=null;
				var status=null;
				var ajax=null;
				if($(a).hasClass('recom')){
					//alert($(a).html());
					setName="推荐商品";
					status=1;
					ajax="recomStatus";
				}else if($(a).hasClass('noRecom')){
					setName="取消推荐";
					status=0;
					ajax="recomStatus";
				}else if($(a).hasClass('best')){
					setName="设置精品";
					status=1;
					ajax="bestStatus";
				}else{
					setName="取消精品";
					status=0;
					ajax="bestStatus";
				}
				var ids = [];
				$('.checkboxes').each(function(){
					if($(this).prop('checked')) ids.push($(this).val());
				})
				//alert(ids);
				if(ids==''){
					layer.msg('请先选择商品！',{icon: 0,time: 2000});
							return;
				}
				layer.confirm('确定'+setName+'？',function(index){
					$.post(ajaxUrl,{id:ids.join(','),ajax:ajax,status:status},function(data){
						if(data){
							layer.msg('已成功'+setName+'！',{icon: 1,time: 2000});
							oTable.fnReloadAjax();
							
						}else{
							layer.msg(setName+'失败！',{icon: 2,time: 2000});
							return;
						}
				   });					
				});	
			}
			
			
			//审核不通过/禁售
			$('#table_common a.no').live('click', function (e) {
				e.preventDefault();
				var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				layer.confirm('确定禁止销售该商品？',function(index){
					$.post(ajaxUrl,{id:aData['goodsId'],ajax:'setStatus',status:0},function(data){
						if(data){
							layer.msg('该商品禁售成功！',{icon: 1,time: 2000});
							oTable.fnReloadAjax();
							
						}else{
							layer.msg('该商品禁售失败！',{icon: 2,time: 2000});
							return;
						}
				   });					
				});	               
            });

			//详情
            $('#table_common a.view').live('click', function (e) {
                e.preventDefault();
                var nRow = $(this).parents('tr')[0];
				var aData = oTable.fnGetData(nRow);
				var url=editUrl+'?goodsId='+aData['goodsId'];
				location.href=url;
            });
        }

    };

}();